package mi.payment.ui.admin;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.settings.SalesSlipPrintSettings;
import mi.payment.settings.SalesSlipPrintSettingsOperation;
import mi.payment.ui.base.ParentActivity;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.dialog.ListDialog;
import sunmi.sunmiui.title.Title;

/**
 * 扫码类交易打印设置界面
 * Created by lj on 2018/1/23.
 */

public class ScanTradePrintActivity extends ParentActivity {
    private View scanCodePrintView;        //扫码类交易是否打印凭条
    private View vScanCodePrinterCount;            //打印凭条单联数

    private TextView rightText3,tScanCodeCountLeftText;
    private ButtonSwitch bsIsPrinterTicket;    //扫码类交易是否打印凭条

    private EditTextDialog mEditTextDialog;

    private ListDialog mListDialog;
    private SalesSlipPrintSettingsOperation mSalesSlipPrintSettingsOperation;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scan_trade_print);
        mSalesSlipPrintSettingsOperation = SalesSlipPrintSettingsOperation.getInstance();
        initTitle();
        initView();
        setClickListener();
        setDataBySelect();
    }

    /**
     * 初始化Title
     */
    private void initTitle() {
        Title mTitle;
        mTitle = (Title) findViewById(R.id.scan_trade_print_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.scan_trade_print));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ScanTradePrintActivity.this.finish();
            }
        }, null, null, null);
    }

    /**
     * 初始化界面
     */
    private void initView() {
        TextView leftText;

        scanCodePrintView = findViewById(R.id.scan_code_print);

        if(Configs.getInstance().IS_SHOW_QR_CODE_PRINT_SALES()){
            scanCodePrintView.setVisibility(View.VISIBLE);
        }else{
            scanCodePrintView.setVisibility(View.GONE);
        }

        vScanCodePrinterCount = findViewById(R.id.scan_code_print_number_setting);

        leftText = (TextView) scanCodePrintView.findViewById(R.id.switch_item_text);
        leftText.setText(getString(R.string.scan_code_print));

        tScanCodeCountLeftText = (TextView) vScanCodePrinterCount.findViewById(R.id.item_left_text);
        tScanCodeCountLeftText.setText(getResources().getString(R.string.print_the_receipt_single_number1));

        rightText3 = (TextView) vScanCodePrinterCount.findViewById(R.id.item_right_text);

        bsIsPrinterTicket = (ButtonSwitch) scanCodePrintView.findViewById(R.id.set_switch);

        setOnCheckedListener();
        vScanCodePrinterCount.setEnabled(bsIsPrinterTicket.isChecked());
        if(bsIsPrinterTicket.isChecked()){
            tScanCodeCountLeftText.setTextColor(getResources().getColor(R.color.C222222));
        }else{
            tScanCodeCountLeftText.setTextColor(getResources().getColor(R.color.C999999));
        }
    }

    /**
     * 设置switch按钮的监听事件
     */
    private void setOnCheckedListener() {
        bsIsPrinterTicket.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mSalesSlipPrintSettingsOperation.saveScanCodePrint(isChecked);
                setDataBySelect();
                vScanCodePrinterCount.setEnabled(isChecked);
                if(isChecked){
                    tScanCodeCountLeftText.setTextColor(getResources().getColor(R.color.C222222));
                }else{
                    tScanCodeCountLeftText.setTextColor(getResources().getColor(R.color.C999999));
                }
            }
        });
    }

    /**
     * 设置item的监听事件
     */
    private void setClickListener() {
        scanCodePrintView.setOnClickListener(this);
        vScanCodePrinterCount.setOnClickListener(this);
    }

    /**
     * 填充数据
     */
    private void setDataBySelect() {
        SalesSlipPrintSettings mSalesSlipPrintSettings = mSalesSlipPrintSettingsOperation.getSalesSlipPrintSettings();
        rightText3.setText(String.valueOf(mSalesSlipPrintSettings.getScanCodeSalesSlip()));
        bsIsPrinterTicket.setOnCheck(mSalesSlipPrintSettings.isScanCodePrint());


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

            case R.id.scan_code_print:
                //扫码类交易是否打印凭条;
                bsIsPrinterTicket.setOnCheck(!bsIsPrinterTicket.isChecked());
                break;

            case R.id.scan_code_print_number_setting:
                //打印凭条单联数
                showEditPrintReceiptNumDialog();
                break;

            default:
                break;
        }
    }

    /**
     * 打印凭条单联数的Dialog
     */
    private void showEditPrintReceiptNumDialog() {
        mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                getString(R.string.save), getString(R.string.print_the_receipt_single_number1), new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mEditTextDialog.dismiss();
                    }
                }, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (checkEmpty(mEditTextDialog.getText()) && checkInt(mEditTextDialog.getText(), 2, 1)) {
                            mSalesSlipPrintSettingsOperation.saveScanCodeSalesSlip(Integer.parseInt(mEditTextDialog.getText()));
                            mEditTextDialog.dismiss();
                            setDataBySelect();
                        }
                    }
                }, new DialogInterface.OnShowListener() {
                    @Override
                    public void onShow(DialogInterface dialog) {
                        mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                        mEditTextDialog.setMaxLength(1);
                        showSoftKeyboard(mEditTextDialog);
                    }
                });
        mEditTextDialog.setText(rightText3.getText().toString());
        mEditTextDialog.show();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeDialog();
    }

    private void closeDialog() {
        try {
            if (mListDialog != null) {
                mListDialog.getDialog().dismiss();
                mListDialog = null;
            }
            if (mEditTextDialog != null) {
                mEditTextDialog.getDialog().dismiss();
                mEditTextDialog = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
